package com.trip.wechat.platform.service.impl;

import com.google.common.base.Strings;
import com.trip.wechat.platform.datastore.MpAccountStore;
import com.trip.wechat.platform.model.AccountBO;
import com.trip.wechat.platform.model.message.PushTemplateMessageResultBO;
import com.trip.wechat.platform.model.message.TemplateMessageBO;
import com.trip.wechat.platform.service.TemplateMessageService;
import com.trip.wechat.platform.service.TokenService;
import com.trip.wechat.platform.subsystem.TemplateMessageFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 功能说明: 模板消息<br>
 * 系统说明: <br>
 * 模块说明: <br>
 * 功能描述: <br>
 * <br>
 */
@Service
public class TemplateMessageServiceImpl implements TemplateMessageService {
    final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Autowired
    MpAccountStore accountStore;
    @Autowired
    TokenService tokenService;
    @Autowired
    TemplateMessageFacade templateMessageFacade;

    @Override
    public PushTemplateMessageResultBO push(String publicKey, TemplateMessageBO message) {
        if (Strings.isNullOrEmpty(publicKey)) {
            LOGGER.info("[TemplateMessageService.push] failed! publicKey cannot be empty!");
//            return createErrorResult(CodeDict.C_SERVICE_FAIL, "publicKey is empty");
        }

        Optional<AccountBO> op = accountStore.get(publicKey);
        if (!op.isPresent()) {
            LOGGER.info("[TemplateMessageService.push] failed! unknown publicKey:{}", publicKey);
//            return createErrorResult(CodeDict.C_SERVICE_FAIL, "unknown publicKey");
        }

        String accessToken = tokenService.token(publicKey);
        if (Strings.isNullOrEmpty(accessToken)) {
            LOGGER.info("[TemplateMessageService.push] failed! fetch token failed!");
//            return createErrorResult(CodeDict.C_SERVICE_FAIL, "token is empty");
        }

        PushTemplateMessageResultBO resultBO = doPush(accessToken, message);
        if (resultBO.getErrcode() == 42001) {   // token is expired
            accessToken = tokenService.refreshToken(publicKey);
            if (Strings.isNullOrEmpty(accessToken)) {
                LOGGER.info("[TemplateMessageService.push] failed! refresh token failed!");
//                return createErrorResult(CodeDict.C_SERVICE_FAIL, "token is empty");
            }
            return doPush(accessToken, message);
        }
        return resultBO;
    }

    protected PushTemplateMessageResultBO doPush(String accessToken, TemplateMessageBO message) {
        return templateMessageFacade.push(accessToken, message);
    }

    protected PushTemplateMessageResultBO createErrorResult(int code, String message) {
        PushTemplateMessageResultBO result = new PushTemplateMessageResultBO();
        result.setErrcode(code);
        result.setErrmsg(message);
        return result;
    }
}
